package com.yzxb.erpsystem.controller;

import com.yzxb.erpsystem.entity.PurchaseOrder;
import com.yzxb.erpsystem.service.PurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/purchase-orders")
public class PurchaseOrderController {
    
    @Autowired
    private PurchaseOrderService purchaseOrderService;
    
    /**
     * 创建采购订单
     */
    @PostMapping
    public ResponseEntity<PurchaseOrder> createPurchaseOrder(@RequestBody PurchaseOrder purchaseOrder) {
        // 请求参数校验
        if (purchaseOrder == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            PurchaseOrder createdOrder = purchaseOrderService.createPurchaseOrder(purchaseOrder);
            return ResponseEntity.status(HttpStatus.CREATED).body(createdOrder);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 更新采购订单
     */
    @PutMapping("/{id}")
    public ResponseEntity<PurchaseOrder> updatePurchaseOrder(@PathVariable Long id, @RequestBody PurchaseOrder purchaseOrder) {
        // 请求参数校验
        if (id == null || purchaseOrder == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            PurchaseOrder updatedOrder = purchaseOrderService.updatePurchaseOrder(id, purchaseOrder);
            return ResponseEntity.ok(updatedOrder);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 删除采购订单
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePurchaseOrder(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            purchaseOrderService.deletePurchaseOrder(id);
            return ResponseEntity.noContent().build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 根据ID查询采购订单
     */
    @GetMapping("/{id}")
    public ResponseEntity<PurchaseOrder> getPurchaseOrderById(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            return purchaseOrderService.getPurchaseOrderById(id)
                    .map(ResponseEntity::ok)
                    .orElse(ResponseEntity.notFound().build());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 根据订单编号查询采购订单
     */
    @GetMapping("/code/{orderCode}")
    public ResponseEntity<PurchaseOrder> getPurchaseOrderByOrderCode(@PathVariable String orderCode) {
        // 请求参数校验
        if (orderCode == null || orderCode.trim().isEmpty()) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            return purchaseOrderService.getPurchaseOrderByOrderCode(orderCode)
                    .map(ResponseEntity::ok)
                    .orElse(ResponseEntity.notFound().build());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 查询所有采购订单
     */
    @GetMapping
    public ResponseEntity<List<PurchaseOrder>> getAllPurchaseOrders() {
        try {
            List<PurchaseOrder> orders = purchaseOrderService.getAllPurchaseOrders();
            return ResponseEntity.ok(orders);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 提交采购订单
     */
    @PutMapping("/{id}/submit")
    public ResponseEntity<PurchaseOrder> submitPurchaseOrder(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            PurchaseOrder submittedOrder = purchaseOrderService.submitPurchaseOrder(id);
            return ResponseEntity.ok(submittedOrder);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 审核采购订单
     */
    @PutMapping("/{id}/approve")
    public ResponseEntity<PurchaseOrder> approvePurchaseOrder(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            PurchaseOrder approvedOrder = purchaseOrderService.approvePurchaseOrder(id);
            return ResponseEntity.ok(approvedOrder);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 拒绝采购订单
     */
    @PutMapping("/{id}/reject")
    public ResponseEntity<PurchaseOrder> rejectPurchaseOrder(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            PurchaseOrder rejectedOrder = purchaseOrderService.rejectPurchaseOrder(id);
            return ResponseEntity.ok(rejectedOrder);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 关闭采购订单
     */
    @PutMapping("/{id}/close")
    public ResponseEntity<PurchaseOrder> closePurchaseOrder(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            PurchaseOrder closedOrder = purchaseOrderService.closePurchaseOrder(id);
            return ResponseEntity.ok(closedOrder);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
}